local lspconfig = require("lspconfig")

lspconfig.gopls.setup({
    on_attach = function(client, bufnr)
        if client.server_capabilities.documentFormattingProvider then
            vim.api.nvim_create_autocmd("BufWritePre", {
                group = vim.api.nvim_create_augroup("LspFormatting", {
                    clear = true
                }),
                buffer = bufnr,
                callback = function()
                    vim.lsp.buf.format({
                        bufnr = bufnr
                    })
                end
            })
        end
        local builtin = require("telescope.builtin") -- ✅

        local opts = {
            buffer = bufnr,
            noremap = true,
            silent = true
        }
        vim.keymap.set("n", "gr", vim.lsp.buf.references, opts)
        vim.keymap.set("n", "gd", builtin.lsp_definitions, opts)
        vim.keymap.set("n", "gi", builtin.lsp_implementations, opts)
        vim.keymap.set("n", "gt", builtin.lsp_type_definitions, opts)

        vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, opts)
        vim.keymap.set("v", "<leader>ca", vim.lsp.buf.code_action, opts)
    end,
    capabilities = require("cmp_nvim_lsp").default_capabilities()
})
